/**
 * 
 */
package com.simpleauth.base;
import java.io.IOException;
import java.util.Map;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

/**
 * @author jtaylor
 *
 */
public class SimpleAuthBase {
	private static final Logger log = Logger.getLogger(SimpleAuthBase.class);
    DatabaseAccess dba;
    
    public SimpleAuthBase () { };
    
	public SimpleAuthBase(String db_uri, String db_type) {
		this.dba = new DatabaseAccess(db_uri, db_type);
	}
	
	public SimpleAuthBase(String db_uri, String db_type, String db_user, String db_pass) {
		this.dba = new DatabaseAccess(db_uri, db_type, db_user, db_pass);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean response = false;
		String authMethod = new String();
        if (args.length < 3) {
        	System.out.println("Usage: java SimpleAuthBase [(authenticate username password | authorize servicename username rolename)]");
        	System.exit(-1);
        }
        SimpleAuthBase auth = new SimpleAuthBase(Constants.mysqlUri, "mysql");
        try {
        	authMethod = args[0];
            if (authMethod.equalsIgnoreCase("authenticate")) {
            	String username = args[1];
            	String password = args[2];
            	response = auth._run_authenticate(username, password);
            } else if (authMethod.equalsIgnoreCase("authorize")) {
            	String servicename = args[1];
            	String username = args[2];
            	String rolename = args[3];
            	response = auth._run_authorize(servicename, username, rolename);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        	System.out.println("To use authenticate, you must pass in username and password!");
        }
        System.out.println("Response for " + authMethod + ": " + response + ".");

	}

	private boolean _run_authenticate(String username, String password) {
		boolean authenticated = false;
		try {
		    authenticated = this.authenticate(username, password);
		} catch (Exception e) {
			System.out.println("Error: " + e);
			e.printStackTrace();
		}
		return authenticated;
    }
	
	private boolean _run_authorize(String servicename, String username, String rolename) {
		boolean authorized = false;
		try {
			authorized = this.authorize(servicename, username, rolename);
		} catch (Exception e) {
			System.out.println("Error: " + e);
			e.printStackTrace();
		}
		return authorized;
    }
	
	private Connection makeConn() {
		Connection con = null;
		
		try {
		    con = dba.getConnection();
		} catch (IOException e) {
			log.debug("Error: " + e);
			System.out.println("Error: " + e);
			e.printStackTrace();
			System.exit(-1);
		}
		return con;
	}
	
	/**
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws IOException
	 * 
	 */
	
	public boolean authenticate(String username, String password)
	    throws Exception {
		// starts assuming false
		boolean authenticated = false;
		Connection con = this.makeConn();

		try {
		    authenticated = dba.getAuthenticated(con, username, password);
		} catch (Exception e) {
			log.debug("Error: " + e);
			System.out.println("Error: " + e);
			e.printStackTrace();
			System.exit(-1);
		}
		con.close();
	    return authenticated;
	}
	
	/**
	 * 
	 * @param servicename
	 * @param username
	 * @param rolename
	 * @return
	 * @throws IOException
	 * 
	 */
	
	public boolean authorize(String servicename, String username, String rolename) 
	    throws Exception {
		boolean authorized = false;
		Connection con = this.makeConn();
		
		try {
		    authorized = dba.getAuthorized(con, servicename, username, rolename);
		} catch (Exception e) {
			log.debug("Error: " + e);
			System.out.println("Error: " + e);
			e.printStackTrace();
			System.exit(-1);
		}
		con.close();
		
		return authorized;
	} 
	
	public Vector getRolesForUser(String servicename, String username)
	    throws Exception {
		Vector roles = new Vector();
		Connection con = this.makeConn();
		
		try {
			roles = dba.getRolesForUser(con, servicename, username);
		} catch (Exception e) {
			log.debug("Error: " + e);
			System.out.println("Error: " + e);
			e.printStackTrace();
			System.exit(-1);
		}
		
		con.close();
		return roles;
	}
}
